package com.ruoyi.question.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.question.domain.DtAnswerRecord;
import com.ruoyi.question.service.IDtAnswerRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 答题记录管理Controller
 *
 * @author llc
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/question/record")
public class DtAnswerRecordController extends BaseController {
    @Autowired
    private IDtAnswerRecordService dtAnswerRecordService;

    /**
     * 查询答题记录管理列表
     */
    @PreAuthorize("@ss.hasPermi('question:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(DtAnswerRecord dtAnswerRecord,
                              @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime,
                              @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        startPage();
        dtAnswerRecord.setBeginTime(beginTime);
        dtAnswerRecord.setEndTime(endTime);
        List<DtAnswerRecord> list = dtAnswerRecordService.selectDtAnswerRecordList(dtAnswerRecord);
        return getDataTable(list);
    }

    /**
     * 导出答题记录管理列表
     */
    @PreAuthorize("@ss.hasPermi('question:record:export')")
    @Log(title = "答题记录管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DtAnswerRecord dtAnswerRecord) {
        List<DtAnswerRecord> list = dtAnswerRecordService.selectDtAnswerRecordList(dtAnswerRecord);
        ExcelUtil<DtAnswerRecord> util = new ExcelUtil<DtAnswerRecord>(DtAnswerRecord.class);
        util.exportExcel(response, list, "答题记录管理数据");
    }

    /**
     * 获取答题记录管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('question:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dtAnswerRecordService.selectDtAnswerRecordById(id));
    }

    /**
     * 新增答题记录管理
     */
    @PreAuthorize("@ss.hasPermi('question:record:add')")
    @Log(title = "答题记录管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DtAnswerRecord dtAnswerRecord) {
        return toAjax(dtAnswerRecordService.insertDtAnswerRecord(dtAnswerRecord));
    }

    /**
     * 修改答题记录管理
     */
    @PreAuthorize("@ss.hasPermi('question:record:edit')")
    @Log(title = "答题记录管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DtAnswerRecord dtAnswerRecord) {
        return toAjax(dtAnswerRecordService.updateDtAnswerRecord(dtAnswerRecord));
    }

    /**
     * 删除答题记录管理
     */
    @PreAuthorize("@ss.hasPermi('question:record:remove')")
    @Log(title = "答题记录管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dtAnswerRecordService.deleteDtAnswerRecordByIds(ids));
    }
}
